<?php
namespace app\admin\controller;
use \think\Controller;
use \think\db;


class Huiz extends Controller
{
    public function hlist (){
        $id = input('param.id');
        if($id){
            db('huiz')->where('hid',$id)->delete();
        }
        $hui = db('huiz')->select();
        $this->assign('hui',$hui);
        return $this->fetch();
    }
    public function hui_add (){
        $hui = input('param.');
        if(!empty($hui)){
          $res =   db('huiz')->insert(['hname'=>$hui['hname']]);
          if($res){
              $this->success('添加成功','Huiz/hlist');
          }
        }
        return $this->fetch();
    }
    public function hui_edit (){
        $res = input('param.id');
        $hui = db('huiz')->where('hid',$res)->find();
        $this->assign('hui',$hui);
        return $this->fetch();
    }
    public function hui_edits (){
        $res = input('param.');
        $hui = db('huiz')->where('hid',$res['id'])->update(['hname' => $res['hname']]);
        if($hui){
            $this->success('修改成功','Huiz/hlist');
        }
        return $this->fetch();
    }
    public function  delAll(Request $request){
        $ids =$request->get('ids');
        $res=db('huiz')->where('hid','in',$ids)->delete();
        //var_dump($res);
        if($res){
            $this->success('删除成功');
        }

    }
    public function index()
    {
        $shequ = db('shequ')->field('sqname,id')->select();
        $user_list =db('user')->field('nickname,head,uid,sex,regtime,phone')->paginate(5);
        $page = $user_list->render();
        $this->assign('user',$user_list);
        $this->assign('page',$page);
        $this->assign('shequ',$shequ);

        $sqid = input('param.shequid');
        if($sqid){
            $user_list =db('user')->where('sqid',$sqid)->field('nickname,head,uid,sex,regtime,phone')->select();
            $this->assign('user',$user_list);
        }
        $huiz = db('huiz')->field('hname,hid')->select();
        $this->assign('huiz',$huiz);
//        $shequ = db('shequ')->field('sqname,id')->select();
        $hui = input('param.username');
        if(!empty($hui)){
            $user = db('user')->where('nickname','like','%'.$hui.'%')->select();
            $this->assign('user',$user);
        }
        return $this->fetch();
    }
    //发布用户通知
    public  function   bhui(){
        $res = input('param.id');

        $huiz = db('huiz')->field('hname,hid')->select();
        $hnum = db('bhui')->where('uid',$res)->field('num')->select();
        $this->assign('huiz',$huiz);
        $this->assign('hnum',$hnum);

        $hui = input('param.');
        if(!empty($hui['huiz'])){
//            var_dump($hui['huiz']);exit();
            $hz = explode(',',$hui['huiz']);
//                        var_dump($hz);exit();
             for($i = 0;$i<count($hz);$i++){
                 $resa = db('bhui')->where('hid',$hz[$i])->where('uid',$hui['id'])->find();
                 if(!empty($resa)){
                     $nums = [
                         'num' => intval($resa['num'])+intval('1')
                     ];
                     db('bhui')->where('hid',$hz[$i])->where('uid',$hui['id'])->update($nums);
                 }
                 else{
                     db('bhui')->insert([
                         'uid' => $hui['id'],
                         'hid' => $hz[$i],
                         'num' => 1
                     ]);
                 }

             }
        }

        return $this->fetch();

//        $data = $request->post();
////        $data['ntime']=time();
//
//        if($data['uid']){
//            $username = db('user')->where('uid','in',$data['uid'])->field('nickname')->select();
//            // var_dump($username);
//            $userN = [];
//            foreach($username as $k => $v){
//                array_push($userN,$v['nickname']);
//            }
//
//            $res = implode(',',$userN);
//            $data['uname'] = $res;
//            $res = db('bhui')->insert($data);
//            if($res){
//                $this->success('发布成功','Bean/unotice');
//            }else{
//                $this->error('发布失败');
//            }
//        }else{
//            $this->error('请选择粉丝');
//        }


    }

}
